package pl.diagnoser.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.BlurbItem;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import pl.diagnoser.client.dto.UserDTO;
import pl.diagnoser.client.tools.DataGetterService;
import pl.diagnoser.client.tools.DataGetterServiceAsync;
import pl.diagnoser.client.tools.LogIn;

public class Diagnoser implements EntryPoint {
	
	private static DataGetterServiceAsync dataGetterAsc = GWT.create(DataGetterService.class);
	private LoginForm loginForm;
	private LoginWindow loginWindow;
	private ApplicationPanel applicationPanel;
	
	public void onModuleLoad() {
		
        if(loginWindow==null)
            loginWindow=new LoginWindow();
        if(!(loginWindow.isVisible() && loginWindow.isDrawn())){
            loginForm.clearValues();
            loginForm.focusInItem("username");
        }
        loginWindow.show();
        loginWindow.bringToFront();
	}
	
	public class LoginWindow extends Window {
		 
        public LoginWindow(){
            loginForm=new LoginForm();
            setShowModalMask(true);
            centerInPage();
            setShowCloseButton(false);
            setShowMinimizeButton(false);
            setIsModal(true);
            setAutoSize(true);
            setShowTitle(true);
            setTitle("Logowanie");
            addItem(loginForm);
        }
    }
	
	private class LoginForm extends DynamicForm {
		
		public LoginForm() {
			BlurbItem blurbItem=new BlurbItem("loginFailure");
	        blurbItem.setVisible(false);
	        blurbItem.setColSpan(2);
	        blurbItem.setDefaultValue("Błędne hasło lub login");
	        blurbItem.setCellStyle("formCellError");
	        
	        TextItem textItem=new TextItem("username");
	        textItem.setTitle("Login");
	        textItem.setTitleOrientation(TitleOrientation.LEFT);
	        textItem.addKeyPressHandler(new KeyPressHandler(){
	            public void onKeyPress(KeyPressEvent keyPressEvent) {
	                if(keyPressEvent.getKeyName().equals("Enter")){
	                    focusInItem("password");
	                }
	            }
	        });
	        
	        PasswordItem passwordItem=new PasswordItem("password");
	        passwordItem.setTitle("Hasło");
	        passwordItem.setTitleOrientation(TitleOrientation.LEFT);
	        passwordItem.addKeyPressHandler(new KeyPressHandler(){
	            public void onKeyPress(KeyPressEvent keyPressEvent) {
	                if(keyPressEvent.getKeyName().equals("Enter")){
	                	//doLogin();
	                }
	            }
	        });
	        
	        ButtonItem buttonItem=new ButtonItem("Login");
	        buttonItem.addClickHandler(new ClickHandler(){
	            public void onClick(ClickEvent clickEvent) {
	            	doLogin();
	            }
	        });
	        
	        setMargin(10);
	        setFields(blurbItem,textItem,passwordItem,buttonItem);
	        
	        //TYMCZAS
	        //textItem.setValue("root");
	        //passwordItem.setValue("root");
	        //doLogin();
		}
		
		private void doLogin() {
			
			if (dataGetterAsc == null) {
				dataGetterAsc = GWT.create(DataGetterService.class);
			}
			
			AsyncCallback<UserDTO> callback = new AsyncCallback<UserDTO>() {
				public void onFailure(Throwable caught) {
					System.out.println(caught.getMessage());
				}

				public void onSuccess(UserDTO result) {
					if(result != null) {
						LogIn.getInstance().setUser(result);
						hideItem("loginFailure");
						applicationPanel = new ApplicationPanel();
						applicationPanel.draw();
						loginWindow.hide();
					}
					else
						showItem("loginFailure");
				}
			};
			
			dataGetterAsc.getUser((String)this.getField("username").getValue(), (String)this.getField("password").getValue(), callback);
		}
	}
}
